module namespace moviesinfo = "http://www.xmlproject.com/smi/moviesinfo";

import module namespace http ="http://www.28msec.com/modules/http";

(: VIEW :)
import module namespace infoview = "http://www.xmlproject.com/smi/lib/view/infoview";

(: CONTROLLER :)
import module namespace getinfo = "http://www.xmlproject.com/smi/lib/controller/getinfo";


declare sequential function moviesinfo:showmovie() {
    declare $title as xs:string := if(empty(http:get-parameters("title")[1]))
                                   then ""
                                   else xs:string(http:get-parameters("title")[1]);

    declare $imdbid as xs:string := if(empty(http:get-parameters("imdbid")[1]))
                                   then ""
                                   else xs:string(http:get-parameters("imdbid")[1]);
                                   
    declare $tmdbid as xs:string := if(empty(http:get-parameters("tmdbid")[1]))
                                   then ""
                                   else xs:string(http:get-parameters("tmdbid")[1]);
                                   
    declare $rtid as xs:string := if(empty(http:get-parameters("rtid")[1]))
                                   then ""
                                   else xs:string(http:get-parameters("rtid")[1]);
    declare $youtubeQuery as xs:string := if(empty(http:get-parameters("title")[1]))
                                   then ""
                                   else xs:string(http:get-parameters("title")[1]);                           
                                   
    (: make sure query is not empty :)
    if ($imdbid = "" and $tmdbid = "" and $rtid = "" and $youtubeQuery = "")
    then 
        block
        {
            
            (: signal Bad Request Error :)
            http:set-status(400); 
            
            (: with the content-type "application/json" the json xml is automatically 
               serialized to valid JSON :)
            http:set-content-type("application/json");
            
            (: this is the xml representation of JSON which makes it easy to generate it, 
               and work with it within sausalito :)
            <json type="object">
                <pair name="status" type="number">400</pair>
                <pair name="statusmessage" type="string">Bad Request</pair>
                <pair name="message" type="string">Please, enter a search term</pair>
            </json>;
        }
    else
            (: browsers should not cache the results :)
            http:set-header("Cache-Control", "no-cache"),
            
            (: with the content type "application/jsonml+json" sausalito
               automatically serializes any xml to jsonml :)
            http:set-content-type("text/html"),
    
            let $imdbResults := getinfo:imdb($imdbid)
            let $theMovieDbResults := getinfo:themoviedb($tmdbid)
            let $rotenTomatoesResults := getinfo:rotentomatoes($rtid)
            let $youtubeResults := getinfo:youtube($youtubeQuery)
            
            (: get the HTML result table for the results :)
            return infoview:showdetails($title, $imdbResults,$theMovieDbResults,$rotenTomatoesResults, $youtubeResults);        
};
(:~
 : handler for the search api. Pass the search term as query parameter.  
 :
 : @return returns the search results from youtube rendered in an HTML table 
:)